package cn.com.lyj6851.modules.shop.service.impl;

import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.modules.shop.domain.YdySystemAttachment;
import cn.com.lyj6851.modules.shop.service.YdySystemAttachmentService;
import cn.com.lyj6851.modules.shop.service.mapper.YdySystemAttachmentMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class YdySystemAttachmentServiceImpl extends BaseServiceImpl<YdySystemAttachmentMapper, YdySystemAttachment> implements YdySystemAttachmentService {

    private final YdySystemAttachmentMapper YdySystemAttachmentMapper;

    /**
     *  根据名称获取
     * @param name name
     * @return YdySystemAttachment
     */
    @Override
    public YdySystemAttachment getInfo(String name) {
        QueryWrapper<YdySystemAttachment> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(YdySystemAttachment::getName,name)
                .last("limit 1");
        return YdySystemAttachmentMapper.selectOne(wrapper);
    }

    /**
     *  根据code获取
     * @param code code
     * @return YdySystemAttachment
     */
    @Override
    public YdySystemAttachment getByCode(String code) {
        QueryWrapper<YdySystemAttachment> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(YdySystemAttachment::getInviteCode,code)
                .last("limit 1");
        return YdySystemAttachmentMapper.selectOne(wrapper);
    }

    /**
     * 添加附件记录
     * @param name 名称
     * @param attSize 附件大小
     * @param attDir 路径
     * @param sattDir 路径
     */
    @Override
    public void attachmentAdd(String name, String attSize, String attDir,String sattDir) {
        YdySystemAttachment attachment =  YdySystemAttachment.builder()
                .name(name)
                .attSize(attSize)
                .attDir(attDir)
                .attType("image/jpeg")
                .sattDir(sattDir)
                .build();
        YdySystemAttachmentMapper.insert(attachment);
    }

    /**
     * 添加附件记录
     * @param name 名称
     * @param attSize 附件大小
     * @param attDir 路径
     * @param sattDir 路径
     * @param uid 用户id
     * @param code 邀请码
     */
    @Override
    public void newAttachmentAdd(String name, String attSize, String attDir, String sattDir,
                                 Long uid, String code) {

        YdySystemAttachment attachment =  YdySystemAttachment.builder()
                .name(name)
                .attSize(attSize)
                .attDir(attDir)
                .attType("image/jpeg")
                .sattDir(sattDir)
                .uid(uid)
                .inviteCode(code)
                .build();
        YdySystemAttachmentMapper.insert(attachment);
    }

}
